package com.codeoss.framework.view.parser;

import org.springframework.stereotype.Service;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.mobile.MNavBar;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service("mnavbarParser")
public class MNavBarParser extends AbstractComponentParser {
	
	@Override
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode,Component component) {
		super.setAttributes(parseContext, objectNode, component);
		MNavBar mNavBar=(MNavBar)component;
		JsonNode jsonNode=objectNode.get("title");
		if(jsonNode!=null) {
			mNavBar.setTitle(jsonNode.asText());
		}
		
		jsonNode=objectNode.get("leftArrowClickExpression");
		if(jsonNode!=null && !"null".equals(jsonNode.asText())&&!StringUtils.isEmpty(jsonNode.asText())) {
			String leftArrowClickExpression=jsonNode.asText();
			if(!StringUtils.isEmpty(leftArrowClickExpression)) {
				String expressionType=Expression.EXPRESSION_TYPE_JAVASCRIPT;
				if(leftArrowClickExpression.startsWith("@")) {
					expressionType=Expression.EXPRESSION_TYPE_SPRING;
				}
				mNavBar.setLeftArrowClickExpression(ExpressionUtil.convertToExpression(leftArrowClickExpression,expressionType));
			}
			
		}
	}
	
	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new MNavBar();
	}

}
